<?php
	//TBC: ham tao combo - ten bang, ten truong la gia tri, ten truong hien thi
	//dieu kien, ten cua combo trong form, index la gia tri ban dau duoc hien thi tren combo
function createCombo($table, $valueField, $textField, $where="", $name, $index){

	global $conn;

	$ret = array();

	//$hide = "status=0";

	$where = $where!="" ? $where : "1=1";

	$result = @mysql_query("select $valueField,$textField from $table where $where",$conn);
	
	echo '<select size="1" name="'.$name.'">';
	
	echo "<option value=''>Chọn giá trị</option>";
	
	while($row=@mysql_fetch_assoc($result)){

		//$ret[] = array($row[$valueField],$row[$textField]);
		
		$selected = $row[$valueField] == $index ? 'selected' : '';
		
		echo "<option value = '".$row[$valueField]."' ".$selected.">".$row[$textField]."</option>";
	}
	echo '</select>';
	//return $ret;
 }
?>
<script language="javascript">
function btnSave_onclick(){
	
	return true;
}
</script>

<?php
	$errMsg ='';
	$img_path = "../upload/images/";
	//$arr = createCombo("tbl_theloai", "id", "tentheloai", "");
	//var_dump($arr);	
	//exit;
?>

<?php
if(isset($_POST['ok_num']))
{
	$num=$_POST['txtnum'];
	echo "<hr />";
	echo "Ban dang chon $num file upload<br />";
	echo "<form action='./' method='post' enctype='multipart/form-data'>";
	echo "<input type='hidden' name='act' value='img_m'>";
	echo "<input type='hidden' name='id' value='".$_REQUEST['id']."'>";
	echo "<input type='hidden' name='page' value='".$_REQUEST['page']."'>";
	for($i=1; $i <= $num; $i++)
	{
		echo "<input type='file' name='img[]' /><br />";
	}
	echo "Thuộc album <br />";
	echo "<input type='text' name='album' /> Or ";
	//TBC: tao combo box load album vao
	createCombo('tbl_album', 'id', 'name', '', 'cbAlbum', '');
	
	//Lưu giá tri so luong hinh anh dc upload
	echo "<input type='hidden' name='nums' value='".$num."' />";
	echo "<br />";echo "<br />";
	echo "<input type='submit' name='btnSave' value='Upload' />";
	echo "</form>";
	echo "<hr />";
}

if (isset($_POST['btnSave']))
{
	$num = $_POST['nums'];
	for($i=0; $i< $num; $i++)
	{
		move_uploaded_file($_FILES['img']['tmp_name'][$i],$img_path.$_FILES['img']['name'][$i]);
		$name=$_FILES['img']['name'][$i];
		if ($_POST['album'] == "")
			$alb_id = $_POST['cbAlbum'];
		else
		{
		}
		$sql = "insert into tbl_image (name, album, file) values ('".$name."', ".$alb_id.", '".$name."')";
		if (mysql_query($sql,$conn)){
			echo '<script>window.location="./?act=img&cat='.$_REQUEST['cat'].'&page='.$_REQUEST['page'].'&code=1"</script>';
		}
		else
		{
			echo "cau lenh bi loi: ".$sql;
		}
	}
	
}

// if (isset($_POST['btnSave'])){
	// $name          = isset($_POST['txtName']) ? trim($_POST['txtName']) : '';
	// $theloaiid          = isset($_POST['cbTheloai']) ? trim($_POST['cbTheloai']) : '';
	// $content          = isset($_POST['txtContent']) ? trim($_POST['txtContent']) : '';
	// $file          = isset($_FILES["fFile"]["name"]) ? trim($_FILES["fFile"]["name"]) : '';
	// $nxb = isset($_POST['txtNXB']) ? trim($_POST['txtNXB']) : '';
	
	// if ($name=="") $errMsg .= "Hãy nhập tên danh mục !<br>";
	// $errMsg .= checkUpload($_FILES['fFile'],".doc;.pdf;.xls;.docx",0, 0);
	// if ($errMsg==''){
		// if (!empty($_POST['id'])){
			// $oldid = $_POST['id'];
			// $sql = "update tbl_document set name='".$name."',theloai_id='".$theloaiid."', content='".$content."', nhaxuatban='".$nxb."' where id=".$oldid;
		// }else{
			// $sql = "insert into tbl_document (name, theloai_id, content, file, nhaxuatban, date_added, last_modified)";
			// $sql .="	values ('".$name."', '".$theloaiid."', '".$content."', '".$file."', '".$nxb."', now(), now())";
		// }
		// echo $sql;
		
		// if (mysql_query($sql,$conn)){
				// @chmod($file_path, 0777);
				// move_uploaded_file($_FILES["fFile"]["tmp_name"],  $file_path. $_FILES["fFile"]["name"]);
				// echo '<script>window.location="./?act=doc&cat='.$_REQUEST['cat'].'&page='.$_REQUEST['page'].'&code=1"</script>';
			// }else{
				// $errMsg = "Không thể cập nhật !";
		// }
	// }

	
		
// }else{
	// if (isset($_GET['id'])){
		// $oldid=$_GET['id'];
		// $page = $_GET['page'];
		// $sql = "select * from tbl_document where id=".$oldid;
		// if ($result = mysql_query($sql,$conn)) {
			// $row=mysql_fetch_array($result);
			// $name          = $row['name'];			
			// $theloaiid = $row['theloai_id'];
			// $file =$row['file'];
			// $nxb = $row['nhaxuatban'];
			
		// }
	// }
// }

?>

<?php
	//$arraySourceCombo    = getArrayCombo('tbl_theloai','id','tentheloai','');

?>
<form method="post" name="frmForm" enctype="multipart/form-data" action="./">

<input type="hidden" name="act" value="img_m">
<input type="hidden" name="id" value="<?=$_REQUEST['id']?>">
<input type="hidden" name="page" value="<?=$_REQUEST['page']?>">
	Enter your Image: <input type="text" name="txtnum" value="<?php echo $_POST['txtnum']; ?>" size="10" />
	<input type="submit" name="ok_num" value="Accept" />	
</form>
<? if($errMsg!=''){echo '<p align=center class="err">'.$errMsg.'<br></p>';}?>